function Export2Nifti(volume, Resolution, filename)
%%  Export 3D Volumes to NIFTI
%
% To export processed volumes to NIFTI for using the data in another
% application, the export script /Export2Nifti/ is provided with Xglom,
% but not visible in the user interface.  Export2Nifti accepts three
% arguments
%  1. a 3D volume (either int or float types)
%  2. 1x3 pixel dimension array
%  3. (optional) output filename.  If this is not used the user is
% asked to create a file using MATLAB's uiputfile dialog.  
%
% The export tool requires the NIFTI MATLAB plugin
% [[http://www.mathworks.com.au/matlabcentral/fileexchange/8797][Tools
% for NIfTI and ANALYZE image]] and the path where it is stored on the
% current system.  This path is set in the Xglom startup script as an
% environment variable.
%
% Type this into the Xglom terminal 
% 
%  >> getenv('NIFTI_PATH')
%
% If the NIFTI path is not set or does not point to a valid path such
% as, NIFTI_PATH=/gpfs/M2Home/projects/Monash038/NIFTI_20121012, use
% the setenv method.
%
% >> setenv('NIFTI_PATH','/gpfs/M2Home/projects/Monash038/NIFTI_20121012')
%
% To export a processed volume (otherwise alled the 'depth' image) of a
% particular dataset. The depth image is stored in the dataset's local
% cache along with the original image and output statistics files.
% Search the directory of dataset 1008.2.18.3.1.1.1 using:
%
% >> ls ./Cache/1008.2.18.3.1.1.1
%   0001.dcm	   output_01.mat       depth_mask_01.mat  
%
% Load the depth mat file
%
% >> load ./Cache/1008.2.18.3.1.1.1/depth_mask_01.mat
%
% The Export2Nifti tool can then be used to write the volume to a *.nii
% file.
%
% >> Export2Nifti(depth, [0.0508 0.0508 0.0508])
%
%   - Michael Eager <michael.eager@monash.edu>


%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.

nifti_path=getenv('NIFTI_PATH')

if ~isdir(nifti_path)
    error(['NIFTI package directory not found. Change path in ~/.Xglomrc ' ...
           'settings or contact the Xglom administrator or change ' ...
           'settings in xglom.sh'])
end
addpath(nifti_path)
if ndims(volume) ~= 3
    error('Export2Nifti: Error, volume not 3D .')
end
if length(Resolution) ~= 3
    error('Export2Nifti: Error, resolution argument length not 3.')
end 
    
if nargin < 3
   [fname, pathname] = uiputfile( {'*.nii', 'NIFTI File (*.nii)'}, 'Save as');
   if isequal(fname,0) || isequal(pathname,0)
       error('Export2Nifti: Error, User pressed cancel for filename.')
   end
   filename = fullfile(pathname,fname);
end


% save nifti
save_nii(make_nii(volume,Resolution),filename);
